package com.yay.mashiro.yayclient.ui.clickgui.panel;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import com.yay.mashiro.yayclient.Yay;
import com.yay.mashiro.yayclient.module.Category;
import com.yay.mashiro.yayclient.module.Module;
import com.yay.mashiro.yayclient.module.Value;
import com.yay.mashiro.yayclient.module.modules.render.ClickGui;
import com.yay.mashiro.yayclient.ui.clickgui.controls.Mode;
import com.yay.mashiro.yayclient.ui.clickgui.controls.Slider;
import com.yay.mashiro.yayclient.ui.clickgui.controls.Switch;
import com.yay.mashiro.yayclient.utils.Colors;
import com.yay.mashiro.yayclient.utils.PlayerUtils;

import ClassSub.Class246;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;

public class WindowPanel {
	
	public static int x;
    public static int y;
    public static int windowWidth;
    public static int windowHeight;
    public static int wheelStateMod;
    public static int wheelStateValue;
    public int tX;
    public int tY;
    public int dragX;
    public int dragY;
    public static double wheelSmoothMod;
    public static double wheelSmoothValue;
    public static boolean isDraging;
    public boolean clickNotDraging = false;
    public Category category;
    public List<ModulePanel> mods = new ArrayList<ModulePanel>();
    public static Map<Value, Switch> booleanValueMap;
    public static Map<Value, Slider> doubleValueMap;
    public static Map<Value, Mode> modeValueMap;

    public WindowPanel(Category category) {
        this.category = category;
        x = -1;
        y = -1;
    }

    public void createModUI() {
        this.mods.clear();
        for (Module mod : Yay.getModuleManager().getModules()) {
            if (mod.getCategory() != this.category) continue;
            this.mods.add(new ModulePanel(mod));
        }
    }

    public void draw(int n, int n2) {
        ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
        windowWidth = (int)((double)scaledResolution.getScaledWidth() * 0.5);
        windowHeight = (int)((double)scaledResolution.getScaledHeight() * 0.6);
        if (windowWidth < 370) {
            windowWidth = 370;
        }
        if (windowHeight < 240) {
            windowHeight = 240;
        }
        if (x == -1 || y == -1) {
            x = scaledResolution.getScaledWidth() / 2 - windowWidth / 2;
            y = scaledResolution.getScaledHeight() / 2 - windowHeight / 2;
        }
        this.processDrag(n, n2);
        this.processWheel(n, n2);
        this.drawBackground();
        this.drawModList();
        this.drawValueList(n, n2);
    }
    
    private void drawValueList(int n, int n2) {
    	
        if (ModulePanel.selectedMod != null) {
            Object object;
            String string;
            Module mod = ModulePanel.selectedMod;
            Yay.getFontManager().usans18.drawString(mod.getRenderName(), x + 120, y + 30, ClickGui.theme.isCurrentMode("Light") ? Colors.BLACK.c : new Color(167, 167, 167).getRGB());
//            if (mod.modButton == null) {
//                mod.modButton = new Switch(mod.getName());
//            }
//            mod.modButton.draw(x + windowWidth - 35, y + 36);
            Class246.drawRect(x + 115, y + 45, x + windowWidth - 15, (float)y + 45.3f, new Color(40, 40, 40).getRGB());
            ArrayList<Value> arrayList = new ArrayList<Value>();
            ArrayList<Value> arrayList2 = new ArrayList<Value>();
            ArrayList<Value> arrayList3 = new ArrayList<Value>();
            arrayList.clear();
            arrayList2.clear();
            arrayList3.clear();
            mod.valueSize = 0;
            for (Value value : Value.list) {
                String string2 = value.getValueName().split("_")[0];
                if (!string2.equalsIgnoreCase(mod.getName())) continue;
                ++mod.valueSize;
                if (value.isValueDouble) {
                    arrayList3.add(value);
                }
                if (value.isValueMode) {
                    arrayList2.add(value);
                }
                if (!value.isValueBoolean) continue;
                arrayList.add(value);
            }
            GL11.glPushMatrix();
            GL11.glEnable((int)3089);
            Class246.doGlScissor(x + 110, y + 45, windowWidth, windowHeight - 55);
            wheelSmoothValue = Class246.getAnimationState(wheelSmoothValue, wheelStateValue * 30, 400.0);
            int n3 = (int)((double)y + wheelSmoothValue);
            //value yOffset
            int n4 = n3 + 60;
            for (Value value : arrayList) {
                string = value.getValueName().split("_")[0];
                object = value.getValueName().split("_")[1];
                Yay.getFontManager().usans16.drawString((String)object, x + 120, n4, ClickGui.theme.isCurrentMode("Light") ? Colors.BLACK.c : new Color(167, 167, 167).getRGB());
                Switch Switch = null;
                if (booleanValueMap.containsKey(value)) {
                    Switch = booleanValueMap.get(value);
                } else {
                    Switch = new Switch(string, (Boolean)value.getValueState());
                    booleanValueMap.put(value, Switch);
                }
                Switch.draw(x + windowWidth - 35, (float)n4 + 5.5f);
                n4 += 30;
            }
            for (Value value : arrayList3) {
                string = value.getValueName().split("_")[1];
                Yay.getFontManager().usans16.drawString(string, x + 120, n4, ClickGui.theme.isCurrentMode("Light") ? Colors.BLACK.c : new Color(167, 167, 167).getRGB());
                object = null;
                if (doubleValueMap.containsKey(value)) {
                    object = doubleValueMap.get(value);
                } else {
                    object = new Slider(value);
                    doubleValueMap.put(value, (Slider)object);
                }
                ((Slider)object).draw(x + windowWidth, n4);
                ((Slider)object).onPress(n, n2);
                n4 += 30;
            }
            for (Value value : arrayList2) {
                string = value.getDisplayTitle();
                Yay.getFontManager().usans16.drawString(string, x + 120, n4, ClickGui.theme.isCurrentMode("Light") ? Colors.BLACK.c : new Color(167, 167, 167).getRGB());
                object = null;
                if (modeValueMap.containsKey(value)) {
                    object = modeValueMap.get(value);
                } else {
                    object = new Mode(value);
                    modeValueMap.put(value, (Mode)object);
                }
                ((Mode)object).draw(x + windowWidth, n4, n, n2);
                n4 += 30 + ((Mode)object).valueOffset;
            }
            if (mod.valueSize == 0) {
                Yay.getFontManager().usans16.drawString("No setting here.", x + 120, y + 60, ClickGui.theme.isCurrentMode("Light") ? new Color(Colors.BLACK.c).brighter().brighter().getRGB() : new Color(167, 167, 167).getRGB());
            }
            GL11.glDisable((int)3089);
            GL11.glPopMatrix();
        }
    }

    private void drawModList() {
    	
    	createModUI();
        GL11.glPushMatrix();
        GL11.glEnable((int)3089);
        Class246.doGlScissor(x + 10, y + 25, 100, windowHeight - 35);
        wheelSmoothMod = Class246.getAnimationState(wheelSmoothMod, wheelStateMod * 30, 400.0);
        int n = (int)((double)y + wheelSmoothMod);
        int n2 = n + 25;
        for (ModulePanel ModulePanel : this.mods) {
            ModulePanel.draw(x + 10, n2);
            n2 += 35;
        }
        GL11.glDisable((int)3089);
        GL11.glPopMatrix();
    }

    private void drawBackground() {
    	ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
        int n;
        int n2 = ClickGui.theme.isCurrentMode("Light") ? -921615 : new Color(27, 27, 27).getRGB();
        int n3 = n = ClickGui.theme.isCurrentMode("Light") ? Colors.RED.c : new Color(47, 116, 253).getRGB();
        if (!ClickGui.theme.isCurrentMode("Light")) {
            Class246.drawRoundedRect((float)x - 0.5f, (float)y - 0.5f, (float)(x + windowWidth) + 0.5f, (float)(y + windowHeight) + 0.5f, 6.0f, new Color(16, 76, 182).getRGB());
        } else {
            Class246.drawRoundedRect((float)x - 1.0f, (float)y - 1.0f, (float)(x + windowWidth) + 1.0f, (float)(y + windowHeight) + 1.0f, 6.0f, n2);
        }
//        x = scaledResolution.getScaledWidth() / 2 - windowWidth / 2;
//        y = scaledResolution.getScaledHeight() / 2 - windowHeight / 2;
//        System.out.println(String.format("x:%s  y:%s  width:%s  height:%s", x, y, windowWidth, windowHeight));
        Class246.drawRoundedRect(x, y, x + windowWidth, y + windowHeight, 6.0f, n2);
        Class246.drawRoundedRect(x + 2, y + 14, x + windowWidth - 2, y + windowHeight - 2, 6.0f, ClickGui.theme.isCurrentMode("Light") ? new Color(Colors.GREY.c).brighter().brighter().getRGB() : new Color(13, 13, 13).getRGB());
        Class246.circle((float)(x + windowWidth - 10), (float)(y + 8), 2.5f, n);
        Yay.getFontManager().raleway16.drawString(this.category.toString(), x + 8, y + 3, ClickGui.theme.isCurrentMode("Light") ? Colors.BLACK.c : new Color(167, 167, 167).getRGB());
    }

    private void processWheel(int n, int n2) {
        int n3 = Mouse.getDWheel();
        if (this.isHover(x + 10, y + 25, 100, windowHeight - 35, n, n2)) {
            if (n3 > 0) {
                if (wheelStateMod < 0) {
                    ++wheelStateMod;
                }
            } else if (n3 < 0 && wheelStateMod * 30 > this.mods.size() * -30) {
                --wheelStateMod;
            }
        } else if (this.isHover(x + 110, y + 25, windowWidth, windowHeight - 35, n, n2)) {
            if (n3 > 0) {
                if (wheelStateValue < 0) {
                    ++wheelStateValue;
                }
            } else if (n3 < 0 && ModulePanel.selectedMod != null && wheelStateValue * 50 > ModulePanel.selectedMod.valueSize * -40) {
                --wheelStateValue;
            }
        }
    }

    private void processDrag(int n, int n2) {
        ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
        if (Mouse.isButtonDown((int)0)) {
            if (this.isHover(x + windowWidth - 17, y, 10, 10, n, n2)) {
                Minecraft.getMinecraft().displayGuiScreen(null);
                return;
            }
            if (this.isHover(x, y, windowWidth, 15, n, n2) || isDraging) {
                isDraging = true;
            } else {
                this.clickNotDraging = true;
            }
            if (isDraging && !this.clickNotDraging) {
                this.dragX = n - this.tX;
                this.dragY = n2 - this.tY;
                x = (double)x < (double)scaledResolution.getScaledWidth() * 0.155 ? (int)((double)scaledResolution.getScaledWidth() * 0.155) + 1 : (x > scaledResolution.getScaledWidth() - windowWidth ? scaledResolution.getScaledWidth() - windowWidth - 1 : (x += this.dragX));
                y = y < 0 ? 1 : (y > scaledResolution.getScaledHeight() - windowHeight ? scaledResolution.getScaledHeight() - windowHeight - 1 : (y += this.dragY));
            }
        } else {
            isDraging = false;
            this.clickNotDraging = false;
        }
        this.tX = n;
        this.tY = n2;
    }

    public boolean isHover(int n, int n2, int n3, int n4, int n5, int n6) {
        return (n5 >= n && n5 <= n + n3 && n6 >= n2 && n6 <= n2 + n4 ? 1 : 0) != 0;
    }

    public void onMouseClick(int n, int n2, int mouseButton) {
        for (ModulePanel object : this.mods) {
            if (!this.isHover(x + 10, y + 25, 100, windowHeight - 35, n, n2) || !object.isPressed(n, n2, mouseButton)) continue;
            for (ModulePanel ModulePanel : this.mods) {
                ModulePanel.selected = false;
            }
            object.selected = true;
            wheelStateValue = 0;
            break;
        }
        for (Switch Switch : booleanValueMap.values()) {
        	if(Switch != ModulePanel.selectedMod.modButton)
            	Switch.isPressed(n, n2, mouseButton);
        }
        if (ModulePanel.selectedMod != null && ModulePanel.selectedMod.modButton != null) {
            ModulePanel.selectedMod.modButton.isPressed(n, n2, mouseButton);
        }
    }

    static {
        isDraging = false;
        booleanValueMap = new HashMap<Value, Switch>();
        doubleValueMap = new HashMap<Value, Slider>();
        modeValueMap = new HashMap<Value, Mode>();
    }
}
